package com.fy.service.service;

import com.fy.order.api.TravelOrderService;
import com.fy.order.dto.FindOrderDto;
import com.fy.order.dto.TravelOrderDto;
import com.fy.service.entity.FindOrderEntity;
import com.fy.service.entity.TravelOrderEntity;
import com.fy.service.mapper.TravelOrderMapper;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TravelOrderServiceImpl implements TravelOrderService {
    @Autowired
    private RedissonClient redissonClient;

    @Resource
    private TravelOrderMapper travelOrderMapper;

    //添加travel_order门票订单信息
    @Override
    @PostMapping(value = "/addTravelOrder")
    public TravelOrderDto addTravelOrder(TravelOrderDto travelOrderDto) {
        TravelOrderEntity travelOrderEntity = new TravelOrderEntity();
        BeanUtils.copyProperties(travelOrderDto,travelOrderEntity);

        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        travelOrderEntity.setCreateTime(day);

        return travelOrderMapper.addTravelOrder(travelOrderEntity);
    }

    //通过id查看travel_order订单
    @Override
    @PostMapping(value = "/travelOrderFindById")
    public TravelOrderDto travelOrderFindById(int travelOrderId) {
        return travelOrderMapper.travelOrderFindById(travelOrderId);
    }

    @Override
    @PostMapping(value = "/travelOrderFindAll")
    public List<TravelOrderDto> travelOrderFindAll() {
        return travelOrderMapper.travelOrderFindAll();
    }

    @Override
    @PostMapping(value = "/findTravelOrder")
    public TravelOrderDto findTravelOrder(FindOrderDto findOrderDto) {
        FindOrderEntity findOrderEntity = new FindOrderEntity();
        BeanUtils.copyProperties(findOrderDto,findOrderEntity);
        return travelOrderMapper.findTravelOrder(findOrderEntity);
    }

    @Override
    @PostMapping(value = "/updateTravelOrder")
    public Boolean updateTravelOrder(TravelOrderDto travelOrderDto) {
        TravelOrderEntity travelOrderEntity = new TravelOrderEntity();
        BeanUtils.copyProperties(travelOrderDto,travelOrderEntity);
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        travelOrderEntity.setCreateTime(day);
        return travelOrderMapper.updateTravelOrder(travelOrderEntity);
    }

    @Override
    @PostMapping(value = "/grabTravelOrder")
    public TravelOrderDto grabTravelOrder(int travelOrderId, int businessId) {

        RLock orderLock = redissonClient.getLock("OrderLock");
        TravelOrderDto travelOrderDto=null;
        try {
            orderLock.lock();
            travelOrderDto=travelOrderMapper.grabTravelOrder(travelOrderId, businessId);
        }finally {
            orderLock.unlock();
        }
        return travelOrderDto;
    }

    @Override
    @PostMapping(value = "/updateTravelOrderState")
    public TravelOrderDto updateTravelOrderState(int travelOrderId, int orderState) {
        return travelOrderMapper.updateTravelOrderState(travelOrderId,orderState);
    }

    @Override
    @PostMapping(value = "/deleteTravelOrderState")
    public int deleteTravelOrderState(int travelOrderId) {
        return travelOrderMapper.deleteTravelOrderState(travelOrderId);
    }

    @Override
    public TravelOrderDto payTravelOrderState(int travelOrderId, int orderState) {
        return travelOrderMapper.payTravelOrderState(travelOrderId,orderState);
    }
}
